package com.ufo.store.management.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.ufo.store.management.entity.SerializableMap;
import com.ufo.store.management.ui.fragment.StockListFragment;

import java.util.List;
import java.util.Map;

/**
 *  作者：taoli on 2017/2/27 0027 09:45
 *  
 */
public class StockViewPagerAdapter extends FragmentPagerAdapter {
    private FragmentManager mFragmentManager;
    private SerializableMap mStockTypeMap;
    private StockListFragment.StockListGetProcessListener listener;
    private int ListStyle;
    private int SearchSortMode;

    public StockViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        mFragmentManager = fragmentManager;
    }

    @Override
    public int getCount() {
        Map<String, String> map = mStockTypeMap.getMap();
        return map.size();
    }

    @Override
    public Fragment getItem(int position) {
        StockListFragment fragment = new StockListFragment();
        fragment.setPosition(position);
        fragment.setStockTypeMap(mStockTypeMap);
        fragment.setStockListStyle(ListStyle);
        fragment.setStockListGetProcessListener(listener);
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Map<String, String> map = mStockTypeMap.getMap();
        String s = "";
        int i = 0;
        for (String key : map.keySet()) {
            if (i == position) {
                s = map.get(key).trim();
                break;
            }
            i++;
        }
        return s;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    public void setStyle(int s){
        ListStyle = s;
        List<Fragment> stockFragments = mFragmentManager.getFragments();
        if (stockFragments != null && stockFragments.size() != 0) {
            for (int i = 0; i < stockFragments.size(); i++) {
                StockListFragment stockFragment = (StockListFragment) stockFragments.get(i);
                if (stockFragment != null && stockFragment.getUserVisibleHint()) {
                    stockFragment.setStyle(s);
                }
            }
        }
    }

    public void setTitleIsHided(boolean is){
        List<Fragment> stockFragments = mFragmentManager.getFragments();
        if (stockFragments != null && stockFragments.size() != 0) {
            for (int i = 0; i < stockFragments.size(); i++) {
                StockListFragment stockFragment = (StockListFragment) stockFragments.get(i);
                if (stockFragment != null && stockFragment.getUserVisibleHint()) {
                    stockFragment.setTitleIsHided(is);
                }
            }
        }
    }

    public void setSearchSortMode(int m){
        SearchSortMode = m;
        List<Fragment> stockFragments = mFragmentManager.getFragments();
        if (stockFragments != null && stockFragments.size() != 0) {
            for (int i = 0; i < stockFragments.size(); i++) {
                StockListFragment stockFragment = (StockListFragment) stockFragments.get(i);
                if (stockFragment != null && stockFragment.getUserVisibleHint()) {
                    stockFragment.setStockSearchSortMode(m);
                }
            }
        }
    }

    public void setStockListStyle(int s){
        ListStyle = s;
    }

    public void setStockTypeMap(SerializableMap map) {
        this.mStockTypeMap = map;
    }

    public void setStockListGetProcessListener(StockListFragment.StockListGetProcessListener lis) {
        this.listener = lis;
    }

    public boolean isFragmentVisible(int position) {
        boolean isV = false;
        List<Fragment> stockFragments = mFragmentManager.getFragments();
        if (stockFragments != null && stockFragments.size() != 0) {
            for (int i = 0; i < stockFragments.size(); i++) {
                StockListFragment stockFragment = (StockListFragment) stockFragments.get(i);
                if (stockFragment != null && stockFragment.getUserVisibleHint()) {
                    if (i == position)
                        isV = true;
                }
            }
        }
        return isV;
    }
}
